import numpy as np
from random import randint


def mark_map(figure, map_m, map_n):
    # 图形的点按照：左上、右上、右下、左下排布
    temp_map = np.zeros((map_n + 1, map_m + 1))
    # 将边界点标记在 map 上
    for kk in range(len(figure)):
        vertex1 = figure[kk, :]
        if kk == len(figure) - 1:
            vertex2 = figure[0, :]
        else:
            vertex2 = figure[kk + 1, :]
        temp_map[vertex1[1], vertex1[0]] = 0.5
        temp_map[vertex2[1], vertex2[0]] = 0.5
        # 沿着x方向扫描
        line_length = abs(vertex2[0] - vertex1[0])
        for kkk in range(1, line_length):
            temp_point_j = min(vertex1[1] + kkk * (vertex2[1] - vertex1[1]) / line_length, map_n)
            temp_point_i = min(vertex1[0] + kkk * (vertex2[0] - vertex1[0]) / line_length, map_m)
            temp_map[int(temp_point_j), int(temp_point_i)] = 0.5  # 边界点定义为0.5
    # 将内部的点标记在 map 上
    for kk in range(map_m + 1):
        index = np.where(temp_map[:, kk] == 0.5)[0]
        if len(index) == 0 or len(index) == 1:
            continue
        else:
            for kkk in range(index[0] + 1, index[1]):
                # 判断是否为边界
                if kk in figure[0, :]:
                    temp_map[kkk, kk] = 0.5
                else:
                    temp_map[kkk, kk] = 1
    return temp_map


def is_overlap(map_1, map_2):
    temp_map = map_1 + map_2
    index = np.where(temp_map == 1.5)
    if len(index[0]) == 0:
        return 0
    else:
        return 1


def is_finish(map_1, map_2):
    temp_map = map_1 + map_2
    index = np.where(temp_map > 0)
    for j in range(1, len(index[1])):
        if index[0][j] == index[0][j - 1]:
            if index[1][j] != index[1][j - 1] + 1:
                return 0
        if index[0][j] == index[0][j - 1] + 1:
            if index[1][j] != index[1][0]:
                return 0
    return 1


# # 初始化
# m = 10  # 大屏幕的长为 30m
# n = 10  # 大屏幕的宽为 20m
#
# # 可以想出一堆图形的组合，然后随机产生
# # 将屏幕分成两个区域，第一个图形在左边、第二个图形在右边
#
# # 先看两个直角三角形
# # 第一个三角形：随机产生左上角的点，再画出来
# side = 4  # 边长为4
# figure1 = np.zeros((3, 2), dtype=int)  # 存储第一个图形各点坐标：横坐标、纵坐标
# figure1[0, :] = [randint(0, int(m / 2) - side - 1), randint(0, n - side - 1)]  # 左上角
# figure1[1, :] = [figure1[0, 0] + side, figure1[0, 1] + side]  # 右下角
# figure1[2, :] = [figure1[0, 0], figure1[0, 1] + side]  # 左下角
# map1 = mark_map(figure1, m, n)
# print(figure1)
# print(map1)
#
# # 第二个三角形：随机产生左下角的点，再画出来
# figure2 = np.zeros((3, 2), dtype=int)
# figure2[0, :] = [randint(int(m / 2) + side, m - 1), randint(0, n - side - 1)]  # 右上角
# figure2[1, :] = [figure2[0, 0], figure2[0, 1] + side]  # 右下角
# figure2[2, :] = [figure2[0, 0] - side, figure2[0, 1] + side]  # 左下角
# map2 = mark_map(figure2, m, n)
# # print(map2)
#
# overlap = is_overlap(map1, map2)
# print(overlap)
# finish = is_finish(map1, map2)
# print(finish)
